package cn.wolfcode.trip.base.service.impl;

import cn.wolfcode.trip.base.domain.Article;
import cn.wolfcode.trip.base.domain.SystemCall;
import cn.wolfcode.trip.base.domain.Travel;
import cn.wolfcode.trip.base.domain.TravelContent;
import cn.wolfcode.trip.base.mapper.SystemCallMapper;
import cn.wolfcode.trip.base.mapper.TravelContentMapper;
import cn.wolfcode.trip.base.mapper.TravelMapper;
import cn.wolfcode.trip.base.query.TravelQuery;
import cn.wolfcode.trip.base.service.ITravelService;
import cn.wolfcode.trip.base.util.JsonResult;
import cn.wolfcode.trip.base.util.UserContext;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class TravelServiceImpl implements ITravelService {

    @Autowired
    private TravelMapper travelMapper;

    @Autowired
    private TravelContentMapper contentMapper;

    @Autowired
    private SystemCallMapper systemCallMapper;

    @Override
    public PageInfo query(TravelQuery qo) {
        //利用第四个参数,取消合理化分页
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize());
        //PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize(),true,false,null);
        List<Travel> list = travelMapper.selectForList(qo);
        return new PageInfo(list);
    }

    @Override
    public void saveOrUpdate(Travel travel) {
        if(travel.getId()!=null){
            travel.setLastUpdateTime(new Date());
            //更新游记基本信息
            travelMapper.updateByPrimaryKey(travel);
            //更新游记内容
            TravelContent travelContent = travel.getTravelContent();
            travelContent.setId(travel.getId());//主键关联
            contentMapper.updateByPrimaryKey(travelContent);
        }else{
            //设置作者
            travel.setAuthor(UserContext.getCurrentUser());
            //保存游记基本信息
            travelMapper.insert(travel);
            //保存游记内容
            TravelContent travelContent = travel.getTravelContent();
            travelContent.setId(travel.getId());//主键关联
            contentMapper.insert(travelContent);
        }
    }

    @Override
    public Travel getById(Long id) {
        return travelMapper.selectByPrimaryKey(id);
    }

    @Override
    public TravelContent getContentById(Long id) {
        return contentMapper.selectByPrimaryKey(id);
    }

    @Override
    public void updateState(Travel travel) {
        //如果是发布状态就需要设置发布时间
        if(travel.getState()==Travel.STATE_RELEASE){
            travel.setReleaseTime(new Date());
        }
        //改变状态时把数据存入系统通知的数据表里面
            SystemCall s=new SystemCall();
            s.setUserId(travel.getAuthor().getId());

            Article article=new Article();
            article.setId(travel.getId());
            article.setContent(travel.getTitle());

            s.setArticle(article);
            s.setState(travel.getState());
            systemCallMapper.insert(s);

        travelMapper.updateState(travel);

    }
}
